{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## Dynamic properties for a class",
   "id": "9d9383d4cace636d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-13T13:29:34.769531Z",
     "start_time": "2024-06-13T13:29:34.758531Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class MyClass:\n",
    "    filed = 1\n",
    "    \n",
    "    def __init__(self):\n",
    "        self.x = 0\n",
    "\n",
    "    def power(self):\n",
    "        return self.x * self.x"
   ],
   "id": "9db5a73c707dc71c",
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-13T13:29:34.784532Z",
     "start_time": "2024-06-13T13:29:34.774531Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = MyClass()\n",
    "b = MyClass()\n",
    "c = MyClass()\n",
    "\n",
    "a.x = 1  # This is equivalent to self.x = 1 for object a\n",
    "b.x = 2  # This is equivalent to self.x = 2 for object b\n",
    "c.x = 3  # This is equivalent to self.x = 3 for object c\n",
    "\n",
    "print(a.power())\n",
    "print(b.power())\n",
    "print(c.power())"
   ],
   "id": "9d6b344384b5502d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "4\n",
      "9\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-13T13:29:34.815533Z",
     "start_time": "2024-06-13T13:29:34.800534Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a.filed = 100  # This is equivalent to a.filed = 100 for object a, not filed = 100 in MyClass\n",
    "print(a.filed)\n",
    "print(b.filed)\n",
    "print(c.filed)"
   ],
   "id": "38c40c8e444b8c20",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n",
      "1\n",
      "1\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-13T13:29:34.831533Z",
     "start_time": "2024-06-13T13:29:34.817531Z"
    }
   },
   "cell_type": "code",
   "source": [
    "MyClass.filed = 100  # This is equal to MyClass.filed = 100\n",
    "print(a.filed)\n",
    "print(b.filed)\n",
    "print(c.filed)"
   ],
   "id": "5c27e14ff6672dad",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n",
      "100\n",
      "100\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### conclusion\n",
    "\n",
    "**Class variables and instance variables are different concepts**: \n",
    "\n",
    "A class variable belongs to the class itself, not to any particular instance of the class, and is shared by all instances of the class, But when we set an attribute of the same name to an instance, the attribute becomes an instance variable of the instance object. So `a.filed = 100` is equivalent to `self.filed = 100` for object a, in function `__init__()`, not `filed = 100` in `MyClass`."
   ],
   "id": "99d3e58f3f6a842b"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
